package com.example.tests;

import static org.hamcrest.MatcherAssert.*;
import static org.hamcrest.Matchers.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import java.util.Set;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class ContactCreationTests extends TestBase {

	Random rnd = new Random();
	
	@DataProvider(name = "randomContacts")
	public Iterator<Object[]> generateRandomContacts(){
		List<Object[]> list = new ArrayList<Object[]>();
		for (int i = 0; i < 5; i++) {
			ContactObject contact = new ContactObject()
			.setFirstName("first name"+rnd.nextInt())
			.setLastName("LastName"+rnd.nextInt())
			.setAddress("Address"+rnd.nextInt())
			.setAddress2("Address2"+rnd.nextInt())
			.setByear(""+rnd.nextInt())
			.setEmail(""+rnd.nextInt())
			.setEmail2(""+rnd.nextInt())
			.setHome(""+rnd.nextInt())
			.setId(""+rnd.nextInt())
			.setMobile(""+rnd.nextInt())
			.setPhone2(""+rnd.nextInt())
			.setWork(""+rnd.nextInt());			
			list.add(new Object[]{contact});
		} 
		return list.iterator();
	}
	
	
	@Test(dataProvider = "randomContacts")
	public void testValidContactCreation(ContactObject validContactObject) throws Exception {
		// action
		Set<ContactObject> oldContactList = app.getContactHelper().getContacts();
		app.getContactHelper().createContact(validContactObject);
		// verification
		Set<ContactObject> newContactList = app.getContactHelper().getContacts();
		verifyContactAdded(oldContactList, validContactObject, newContactList);
	}
	
private void verifyContactAdded(Set<ContactObject> oldContactList,
			ContactObject validContact, Set<ContactObject> newContactList) {
			assertThat(newContactList.size(), equalTo(oldContactList.size()+1));
			validContact.setId("$$$");
			oldContactList.add(validContact);
			validContact.setId(null);
			assertThat(newContactList, equalTo(oldContactList));
	}

	@Test
	public void testContactWithEmptyDataCanBeCreated() throws Exception {
		ContactObject emptyDataContact = new ContactObject()
				.setFirstName("").setLastName("").setAddress("")
				.setHome("").setMobile("").setWork("")
				.setEmail("").setEmail2("").setByear("").setAddress2("")
				.setHome("").setId("id");
		app.getContactHelper().createContact(emptyDataContact);
	}
		
}
